var fields = new Array("firstname","lastname","address",
					"city","state","zipcode","dayphone","eveningphone","email","cellphone");
function validate(frm) {
	var errmsg = $("errmsgs");
	var err = $("error");

	var errorcount = 0;
	var errmsgs = '';

	if(!IsNumeric(frm.cost.value)) {
		errmsgs += 'Check the value of appointment cost.<br />';
		errorcount++;
	}

	if(!IsNumeric(frm.tip.value)) {
		errmsgs += 'Check the value of tip.<br />';
		errorcount++;
	}
	
	if(frm.customer_id=="") {
		if(IsBlank(frm.firstname.value)) {
			errmsgs += 'Please enter customer\'s first name.<br />';
			errorcount++;
		}

		if(IsBlank(frm.lastname.value)) {
			errmsgs += 'Please enter customer\'s last name.<br />';
			errorcount++;
		}
			
		if(IsBlank(frm.email.value)) {
			errmsgs += 'Please enter customer\'s email address.<br />';
			errorcount++;
		} else if(!checkMail(frm.email.value)) {
			errmsgs += 'Email address is not in proper format.<br />';
			errorcount++;
		}	

	}

	if(IsBlank(frm.apptstatus.value)) {
		errmsgs += 'Please select a status.<br />';
		errorcount++;
	}

	if(IsBlank(frm.service_id.value)) {
		errmsgs += 'Please select a service.<br />';
		errorcount++;
	}

	if(errorcount > 0) {
		errmsg.innerHTML = errmsgs;
		err.style.display = "block";
	} else {
		err.style.display = "none";
	}
	
	if(errorcount == 0 ) {
			var url = 'codelibrary/inc/ajax-finalize.php';
			finalizeAjax(frm,url);
	}
	return false;
}

function checkMail(x) {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) return true;
	else return false;
}

function trim(a) {
	a = a.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
}

function IsBlank(txt) {
	txt = trim(txt);
	if(txt == "")
		return true;
	else
		return false;
}

function IsNumeric(strString)
   {
   var strValidChars = "0123456789.-,";
   var strChar;
   var blnResult = true;
	
   if (strString.length == 0) return true;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
 }
   
function finalizeAjax(frm,url) {
	var pars = 'newcustomer=' + frm.customer_id.value;
	var params = '';
	var errmsg = $("errmsgs");
	var err = $("error");
	var rand = new Date();
	var time = rand.getTime();
	if(frm) {		
		params = params + getFormValues(frm) + '&time='+time;
	}
	pars = pars + params;

	new Ajax.Request(url, {
    parameters: pars,

    onComplete: function (req) {
		var response = req.responseText;
		//alert(response);

		var xmlDoc=loadXMLString(response);

		var status = getNode("status");
		var msg = getNode("msg");
		if(status == 1) {
			//location.href="finalize-confirm.php";
			showAppAddMessage(msg);
		} else {
			
			if(msg=='') {
				msg = response;
			}
			errmsg.innerHTML = msg+"<br />";
			err.style.display="block";
		}
		showLoading(0);
    },
	onLoading: function(req) {
		showLoading(1);
	},
	onError: function (req) {
		showLoading(0);
	}

  });
}

function ajaxGetCustomer(customerid) {
	var url = 'codelibrary/inc/ajax-getcustomer.php?customer_id='+customerid;
	var rand = new Date()
	var time = rand.getTime();
	var pars = '&time='+time;
	if(customerid==0) {
		emptyCustomer();
		return false;
	}

	new Ajax.Request(url, {
    parameters: pars,
	
    onComplete: function (req) {
		var response = req.responseText;
		//alert(response);
		var xmlDoc=loadXMLString(response);
		var status = getNode("status");
		if(status == 1) {
			for (var i=0;i<fields.length;i++) {
				$(fields[i]).value = getNode(fields[i]);
				$(fields[i]).readOnly=true;
			}
		} else {
			var msg = getNode("msg");
			var msg = getNode("msg");
			errmsg.innerHTML = errmsg.innerHTML + msg + "<br />";
			err.style.display="block";
		}
		showLoading(0);
    },
	onLoading: function(req) {
		showLoading(1);
	},
	onError: function (req) {
		showLoading(0);
	}

	});
	showLoading(0);
}

function ajaxGetService(serviceid) {
	if(serviceid=='')
		return false;

	var url = 'codelibrary/inc/ajax-getservice.php?service_id='+serviceid;
	var rand = new Date()
	var time = rand.getTime();
	var pars = '&time='+time;

	new Ajax.Request(url, {
    parameters: pars,
	
    onComplete: function (req) {
		var response = req.responseText;
		//alert(response);
		var xmlDoc=loadXMLString(response);
		var status = getNode("status");

		if(status == 1) {
			$("duration").value = getNode("duration");
			$("cost").value = getNode("cost");
		} else {
			var msg = getNode("msg");
			var msg = getNode("msg");
			errmsg.innerHTML = errmsg.innerHTML + msg + "<br />";
			err.style.display="block";
		}
		showLoading(0);
    },
	onLoading: function(req) {
		showLoading(1);
	},
	onError: function (req) {
		showLoading(0);
	}

	});
	showLoading(0);
}	

function getNode(name) {
   if(xmlDoc.getElementsByTagName(name)[0]) {
	if(xmlDoc.getElementsByTagName(name)[0].childNodes[0]) {
		return xmlDoc.getElementsByTagName(name)[0].childNodes[0].nodeValue;
    }
   }
  return '';
}

function emptyCustomer() {
	for (var i=0;i<fields.length;i++) {
		$(fields[i]).value='';
		$(fields[i]).readOnly=false;
	}
}



function getFormValues(frm) {
	var params = '';
	for(i=0;i<frm.elements.length;i++) {
		var type = frm.elements[i].type;
		var flag = false;
		if(type !="checkbox" && type!="radio") {
			params = params + '&' + frm.elements[i].name + '=' + frm.elements[i].value;
		} else if(type == "checkbox" || type == "radio") {
			if(frm.elements[i].checked) {
				params = params + '&' + frm.elements[i].name + '=' + frm.elements[i].value;
				flag = true;
			} else {
				var elename = frm.elements[i].name;
			}
			
		} 
	}

	if(!flag && elename){
	   params = params + '&' + elename + '=';
    }

	
	return params;
}

function showAppAddMessage(msg) {
	var frm = $("formcontent");
	var compl = $("apptadded");
	var finalize = $("finalize");
	frm.innerHTML = compl.innerHTML;

	$("apptmsg").innerHTML=msg;
	finalize.disabled = true;
}

